//
//  SettingVC.swift
//  设置
//
//  Created by 千策万划-mini on 2025/4/28.
//

import Foundation

class SettingVC: BaseTableVC {
    
    // MARK: - lazy
    lazy var viewNav: BaseNavView = {
        let result = BaseNavView()
        result.resetNavTitle(title: "Settings", leftView: nil, rightView: nil)
        result.leftTop = XY(0, 0)
        return result
    }()
    lazy var viewContent: SettingContentView = {
        let result = SettingContentView()
        result.blockLogoutClick = { [weak self] in
            guard let self = self else { return }
            self.viewLogout.show()
        }
        result.blockCancelAccountClick = { [weak self] in
            guard let self = self else { return }
            self.viewCancelAccount.show()
        }
        return result
    }()
    lazy var viewLogout: LogoutView = {
        let result = LogoutView()
        result.blockConfirmLogout = { [weak self] in
            guard let self = self else { return }
            self.logout()
        }
        return result
    }()
    lazy var viewCancelAccount: CancelAccountView = {
        let result = CancelAccountView()
        result.blockConfirmCancelAccount = { [weak self] in
            guard let self = self else { return }
            self.cancelAccount()
        }
        return result
    }()
    
    // MARK: - viewDidLoad
    override func viewDidLoad() {
        super.viewDidLoad()
        configUI()
    }
    
    func configUI() {
        viewBg.backgroundColor = COLOR_BACKGROUND
        tableView.backgroundColor = .clear
        tableHeaderBackgroundView.backgroundColor = .clear
        view.addSubview(viewNav)
        tableView.height = SCREEN_HEIGHT-viewNav.bottom-86
        tableView.top = viewNav.bottom
        tableView.tableHeaderView = viewContent
    }
    
    // MARK: - request
    // 退出
    func logout() {
        LoginRequestApi.logout { request, responseData in
            self.viewLogout.hide()
            GlobalMethod.relogin()
        } failure: { request, error in
            
        }
    }
    
    // 注销
    func cancelAccount() {
        if viewCancelAccount.btnAgree.isSelected == false {
            GlobalMethod.showAlert(strAlert: "Please read the above carefully")
            return
        }
        LoginRequestApi.cancelAccount { request, responseData in
            GlobalMethod.showAlert(strAlert: "Logout succeeded")
            self.viewCancelAccount.hide()
            GlobalMethod.relogin()
        } failure: { request, error in
        
        }
    }
}
